﻿Public Class Frm_Envois
    Dim isDateValidate As Boolean = True
    Dim Dat_envoi As String
    Dim enve As Envoi
    Dim plnt As Plainte

    Public Sub Remplir_list()
        Try
            Partager.moi.remplir_list_envoi()
            Frm_Plainte.plnte.remplir_Plainte_envoi()

            List_tous.Items.Clear()
            For Each plEn As Plainte_Envoi In plnt.list_Plainte_envoi
                For Each env As Envoi In Partager.moi.list_envoi
                    If plEn.id_envoi = env.id Then
                        List_tous.Items.Add(New ListViewItem(New String() {env.id, env.Num_Envoi, env.Date_Envoi, env.Direction, env.Dicision}))
                        Exit For
                    End If
                Next
                Exit For
            Next

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
       
    End Sub
    'Tarik version proce
    Public Sub remplirLstEnvoiProc()
        Try
            List_tous.Items.Clear()
            With Partager.procSelected
                .remplirLstEnvois()
                For Each env In .envois
                    List_tous.Items.Add(New ListViewItem(New String() {env.id, env.Num_Envoi, env.Date_Envoi, env.Direction, env.Dicision}))
                Next
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub txt_envoi_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_envoi.KeyPress
        Dim allowedChars As String = "1234567890/"
        If e.KeyChar = ChrW(8) Then
            e.Handled = False
        ElseIf allowedChars.IndexOf(e.KeyChar) = -1 Then
            ' Invalid Character
            e.Handled = True
        End If
    End Sub
    Private Sub date_envoi_TypeValidationCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.TypeValidationEventArgs) Handles date_envoi.TypeValidationCompleted
        If (Not e.IsValidInput) Then
            If (Not e.IsValidInput) Then
                If date_envoi.Text <> "  /  /" Then
                    Me.ToolTip1.ToolTipTitle = "تارخ خاطئ !"
                    Me.ToolTip1.Show("هل تسخر مني >_< أدخل التاريخ على هذا الشكل: السنة/الشهر/اليوم", Me.date_envoi, 0, -50, 5000)
                    isDateValidate = False
                End If
            End If
            If date_envoi.Text = "  /  /" Then
                isDateValidate = True
            End If
        Else
            isDateValidate = True
        End If
    End Sub

    Private Sub Btn_parc_direction_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_parc_direction.Click
        Me.Enabled = False
        Frm_Direction.Show()
    End Sub

    Private Sub Frm_Envois_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        If Partager.viensDeProc Then
            frmProces.Enabled = True
            Partager.viensDeProc = False
        Else
            Frm_Plainte.Enabled = True
        End If
    End Sub

    Private Sub Frm_Envois_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ToolTip1.IsBalloon = True

        Partager.com_ville(Comb_direction, "Comb_direction", "direction")
        '-----------------------------------------------
        If Partager.viensDeProc = True Then
            remplirLstEnvoiProc()
        Else
            plnt = Frm_Plainte.plnte
            Remplir_list()
            If List_tous.Items.Count > 0 Then
                Btn_ajout.Enabled = False
            End If
        End If
    End Sub

    Private Sub Btn_nouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_nouv.Click
        Partager.vider(Me)
        txt_envoi.Select()
    End Sub

    Private Sub Btn_ajout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajout.Click
        Try
            If (List_tous.SelectedIndices.Count > 0) Then
                MsgBox("عفوا لا يمكنك الإضافة، لإضافة إرسال جديد عليك الضغط على الزر جديد ثم إدخال البيانات", MsgBoxStyle.Critical, "خطأ في الإضافة")
            Else
                    If MsgBox("هل تريد فعلا إضافة هذا الارسال ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة إرسال") = MsgBoxResult.Yes Then


                        If isDateValidate Then
                            If date_envoi.Text = "  /  /" Then
                                Dat_envoi = Nothing
                            Else
                                Dat_envoi = date_envoi.Text
                                Dat_envoi = Partager.reverseString(Dat_envoi)
                            End If
                            '--------------------------------------------------------------
                            Dim env As New Envoi(1, txt_envoi.Text, Dat_envoi, Comb_direction.Text, txt_dicision.Text, txt_observ.Text)
                            Partager.moi.Ajouter_envoi(env)
                            '--------------------------------------------------------------
                            Dim lastItem As Integer = Partager.moi.list_envoi.Count - 1
                            enve = CType(Partager.moi.list_envoi.Item(lastItem), Envoi)
                            '--------------------------------------------------------------
                            If Partager.viensDeProc Then
                                'Tarik
                                Partager.procSelected.AjouterEnvoi(enve.id)
                                remplirLstEnvoiProc()
                            Else
                                Dim plnEnv As New Plainte_Envoi(plnt.Id, enve.id)
                                plnt.Ajouter_Plainte_envoi(plnEnv)
                                '--------------------------------------------------------------
                                Remplir_list()
                            End If
                            '--------------------------------------------------------------
                            Partager.vider(Me)
                            txt_envoi.Select()
                            Btn_ajout.Enabled = False
                            '-------------------------------------------------------------
                            MsgBox("تمت إضافة الإرسال بنجاح", MsgBoxStyle.Information, "نجاح")
                        Else
                            MsgBox("إن هذا التاريخ الذي أدخلت خاطئ", MsgBoxStyle.Critical, "خطأ")
                        End If
                    End If
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_modif_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_modif.Click
        Try
            If List_tous.SelectedIndices.Count > 0 Then

                If txt_envoi.Text <> "" Then
                    If MsgBox("هل تريد فعلا تعديل هذا الارسال ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "تعيل شكاية") = MsgBoxResult.Yes Then


                        If isDateValidate Then
                            If date_envoi.Text = "  /  /" Then
                                Dat_envoi = Nothing
                            Else
                                Dat_envoi = date_envoi.Text
                                Dat_envoi = Partager.reverseString(Dat_envoi)
                            End If
                            '--------------------------------------------------------------
                            Dim env As New Envoi(1, txt_envoi.Text, Dat_envoi, Comb_direction.Text, txt_dicision.Text, txt_observ.Text)
                            Partager.moi.modifier_envoi(env, enve.id)
                            '--------------------------------------------------------------
                            If Partager.viensDeProc Then
                                remplirLstEnvoiProc()
                            Else
                                Remplir_list()
                            End If
                            '--------------------------------------------------------------
                            Partager.vider(Me)
                            txt_envoi.Select()
                            '-------------------------------------------------------------
                            MsgBox("تم تعديل الإرسال بنجاح", MsgBoxStyle.Information, "نجاح")
                        Else
                            MsgBox("إن هذا التاريخ الذي أدخلت خاطئ", MsgBoxStyle.Critical, "خطأ")
                        End If
                    End If
                Else
                    MsgBox("عفوا، عليك إدخال البيانات", MsgBoxStyle.Critical, "خطأ")
                End If
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub



    Private Sub Btn_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_supp.Click
        Try
            If List_tous.SelectedIndices.Count > 0 Then
                If MsgBox("هل تريد فعلا حذف هذا الارسال ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف ارسال") = MsgBoxResult.Yes Then
                    Partager.close()
                    If Partager.viensDeProc Then
                        'Tarik
                        Dim idEnvoi As Integer = CType(List_tous.SelectedItems(0).SubItems(0).Text, Integer)
                        For Each en In Partager.procSelected.envois
                            If idEnvoi = en.id Then
                                Partager.procSelected.supprimerEnvoi(idEnvoi)
                                Exit For
                            End If
                        Next
                        remplirLstEnvoiProc()
                    Else
                        'Dim ev As Envoi = Partager.moi.list_envoi(List_tous.SelectedIndices.Item(0))
                        plnt.remplir_Plainte_envoi()
                        For Each plEn In plnt.list_Plainte_envoi
                            Partager.moi.suprimmer_envoi(plEn.id_envoi)
                            Exit For
                        Next
                        '--------------------------------------------------------------
                        Remplir_list()
                    End If
                    '--------------------------------------------------------------
                    Partager.vider(Me)
                    txt_envoi.Select()
                    Btn_ajout.Enabled = True
                    '--------------------------------------------------------------
                    MsgBox("عملية الحذف تمت بنجاح", MsgBoxStyle.Information, "نجاح")
                End If
            Else
                MsgBox("اختر ارسال من القائمة", MsgBoxStyle.Information, "خطأ")
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub List_tous_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_tous.SelectedIndexChanged
        Try
            If Partager.viensDeProc Then
                'Tarik
                If List_tous.SelectedItems.Count > 0 Then
                    Dim idEnvoi As Integer = CType(List_tous.SelectedItems(0).SubItems(0).Text, Integer)
                    For Each en In Partager.procSelected.envois
                        If idEnvoi = en.id Then
                            enve = en
                            txt_envoi.Text = en.Num_Envoi
                            txt_dicision.Text = en.Dicision
                            txt_observ.Text = en.observ
                            Comb_direction.Text = en.Direction
                            date_envoi.Text = en.Date_Envoi
                            Exit For
                        End If
                    Next
                End If
            Else
                'ayoub
                plnt.remplir_Plainte_envoi()
                For Each plEn In plnt.list_Plainte_envoi
                    For Each en As Envoi In Partager.moi.list_envoi
                        If plEn.id_envoi = en.id Then
                            enve = en
                            txt_envoi.Text = en.Num_Envoi
                            txt_dicision.Text = en.Dicision
                            txt_observ.Text = en.observ
                            Comb_direction.Text = en.Direction
                            date_envoi.Text = en.Date_Envoi
                        End If
                    Next
                    Exit For
                Next
            End If

            If List_tous.SelectedIndices.Count <= 0 And Not Partager.doubleVider Then
                Partager.vider(Me, New ArrayList From {"List_tous"})
                txt_envoi.Select()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

   End Class